package com.fruit.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

import java.util.Arrays;

/**
 * C - Cross  O - Origin  R - Resource  S - Sharing 跨域资源共享
 *
 * @Description: 跨域配置
 * @Author: LJ
 * @Date: 2018/11/8 10:58
 * @version:
 */
@Configuration
public class CorsConfig {

    @Bean
    public CorsFilter corsFilter() {
        final UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        final CorsConfiguration config = new CorsConfiguration();

        config.setAllowCredentials(true); //是否支持cookie跨域
        config.setAllowedOrigins(Arrays.asList("*")); //http:www.a.com 原始域
        config.setAllowedHeaders(Arrays.asList("*")); //允许头
        config.setAllowedMethods(Arrays.asList("*")); //调用方式
        config.setMaxAge(300L); //缓存时间300秒 相同的跨域请求不验证

        source.registerCorsConfiguration("/**", config); //跨域的路径
        return new CorsFilter(source);
    }
}
